<?php
/**
 * 定时任务 - 15天订单自动收货
 */
namespace Core\Controller;
use Think\Controller;
use  Common\Logic\GroupbuyLogic;

header("content-type:application/json");
set_time_limit(0);

class OrderTaskController extends Controller 
{
    public function index() 
    {
        echo "请使用新方法";
        return false;
        $orderModel = M('order');
        $where['shipping_status'] = array('eq', 1);
        $where['parent_sn'] = array('neq', '0');
        $field = 'order_id, order_sn, add_time';
        $order_list = $orderModel->field($field)->where($where)->select();
        
        //15天时间戳
        $last_time = 15 * 24 * 3600;
        foreach ($order_list as $key => $value) {
            if (($value['add_time'] + $last_time) < time()) {
                $orderModel->where(array('order_id'=>$value['order_id']))->setField('order_status', 2);
                order_log($value['order_sn'], '', '系统默认收货', 2);
            }
        }

    }
    // 超时订单
    public function outtime() 
    {
        
        $res = \Home\Logic\OrderLogic::check_order_outtime();

        echo \json_encode($res);
    }

    // 定时清空购物车垃圾数据
    public function cartRubsh()
    {
        $time = time() - 3600; // 删除购物车数据  1小时以前的
        // M("Cart")->where("user_id = 0 and  add_time < $time")->delete();            
        $res = M("Cart")->where([
            "user_id" => 0,
            "add_time" => ["lt", $time],
        ])->delete();

        echo \json_encode([
            "count" => $res
        ]);
    }

    // 定时自动完成订单收货
    public function autoConfirm()
    {

        $date = tpCache("shopping.auto_confirm_date");
        $date = $date > 0 ? $date : 14;
        $last_time = time() - $date * 86400;

        $where['shipping_status'] = 1;
        $where['order_status'] = 1;
        $where['shipping_time'] = ['lt',$last_time];
        $result = [];
        $order_id_arr = M('order')
            ->where($where)
            ->field('order_id, order_sn, add_time')->select();
        foreach($order_id_arr as $k => $v)
        {
            $result[$v['order_sn']] = confirm_order($v['order_id'],0,"系统自动确认收货");
        }
        echo \json_encode($result);
    }

    // 定时自动发放分销收益
    public function autoDispatch()
    {
        
        $switch = tpCache('distribut.switch');
        if( $switch != 1 )
        {
            return json_encode([
                "msg" => "分销已关闭"
            ]);
        }
        if( !file_exists(APP_PATH.'Common/Logic/DistributLogic.class.php'))
        {
            return json_encode([
                "msg" => "处理方法不存在"
            ]);
        }

        $distributLogic = new \Common\Logic\DistributLogic();
        $res = $distributLogic->auto_confirm(); // 自动确认分成

        echo \json_encode($res);
    }
 
    /**
     * 自动处理团购订单
     */
    public function destoryGroupOrder()
    {
        #找到5分钟前结束的团购
        // $groups = M('group_info')
        // ->where([
        //     "end_time" => ["lt",time()-300],
        //     "status" => 0
        // ])->select();

        // $res = [];

        // foreach($groups as $k => $v)
        // {
        //     $res[$v['id']] = GroupbuyLogic::checkGroupBuy();
        // }

        $res = GroupbuyLogic::checkGroupBuy();

        echo \json_encode($res);

    }
    
}